package com.seadragon.app.ksr.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.seadragon.app.ksr.dao.CustomerDao;
import com.seadragon.app.ksr.model.Address;
import com.seadragon.app.ksr.model.Customer;
import com.seadragon.app.ksr.service.AddressService;
import com.seadragon.app.ksr.service.CustomerService;
import com.seadragon.app.ksr.service.GeocodeService;
import com.seadragon.app.ksr.service.SearchService;
import com.seadragon.app.ksr.util.IndexManager;

@Service("customerService")
@Transactional
public class CustomerServiceImpl implements CustomerService {
	private Logger logger = LoggerFactory.getLogger(CustomerServiceImpl.class);
	
	@Autowired
	private CustomerDao customerDao;
	
	@Autowired
	private SearchService searchService;
	
	@Autowired
	private AddressService addressService;
	
	@Autowired
	private GeocodeService geocodeService;
	
	@Autowired
	private IndexManager indexManager;
	
	@Override
	public Customer update(Customer customer) {
		Customer customer2 = customerDao.findByEmail(customer.getEmail());
		if(customer.getFirstName() != null && customer.getFirstName().length() > 0 
				&& !customer.getFirstName().equalsIgnoreCase(customer2.getFirstName())){
			customer2.setFirstName(customer.getFirstName());
		}
		if(customer.getLastName() != null && customer.getLastName().length() > 0 
				&& !customer.getLastName().equalsIgnoreCase(customer2.getLastName())){
			customer2.setLastName(customer.getLastName());
		}
		boolean addressChanged = false;
		Address residentAddress = customer.getResidentAddress();
		Address residentAddress2 = customer2.getResidentAddress();
		if(residentAddress.getAddressLine1() != null && residentAddress.getAddressLine1().length() > 0 
				&& !residentAddress.getAddressLine1().equalsIgnoreCase(residentAddress2.getAddressLine1())){
			addressChanged = true;
		}
		if(residentAddress.getAddressLine2() != null && residentAddress.getAddressLine2().length() > 0 
				&& !residentAddress.getAddressLine2().equalsIgnoreCase(residentAddress2.getAddressLine2())){
			addressChanged = true;
		}
		if(residentAddress.getCity() != null && residentAddress.getCity().length() > 0 
				&& !residentAddress.getCity().equalsIgnoreCase(residentAddress2.getCity())){
			addressChanged = true;
		}
		if(residentAddress.getState() != null && residentAddress.getState().length() > 0 
				&& !residentAddress.getState().equalsIgnoreCase(residentAddress2.getState())){
			addressChanged = true;
		}
		if(residentAddress.getZip() != null && residentAddress.getZip().length() > 0 
				&& !residentAddress.getZip().equalsIgnoreCase(residentAddress2.getZip())){
			addressChanged = true;
		}
		if(addressChanged){
			Address matchedAddress = searchService.searchAddress(residentAddress);
			if(matchedAddress == null){
				geocodeService.geocode(residentAddress);
				customer2.setResidentAddress(residentAddress);
			}else{
				customer2.setResidentAddress(matchedAddress);
			}
		}
		if(customer.getInterest() != null && customer.getInterest().length() > 0 && customer.getInterest().equalsIgnoreCase(customer2.getInterest())){
			customer2.setInterest(customer.getInterest());
		}
		if(customer.getPhoneNumber() != null && customer.getPhoneNumber().length() > 0 
				&& !customer.getPhoneNumber().equalsIgnoreCase(customer2.getPhoneNumber())){
			customer2.setPhoneNumber(customer.getPhoneNumber());
		}
		if(customer.getDateOfBirth() != null && (customer2.getDateOfBirth() == null || customer.getDateOfBirth().compareTo(customer2.getDateOfBirth()) != 0)){
			customer2.setDateOfBirth(customer.getDateOfBirth());
		}
		customerDao.update(customer2);
		try{
			indexManager.insertDocument(customer2);
		}catch(Exception ex){
			logger.error(ex.getMessage(), ex);
		}

		return customer2;
	}

	@Override
	public void save(Customer customer) {
		Address matchedAddress = searchService.searchAddress(customer.getResidentAddress());
		if(matchedAddress == null){
			geocodeService.geocode(customer.getResidentAddress());
		}else{
			customer.setResidentAddress(matchedAddress);
		}
		customerDao.save(customer);
		try{
			indexManager.insertDocument(customer);
		}catch(Exception ex){
			logger.error(ex.getMessage(), ex);
		}
	}

	@Override
	public Customer findByEmail(String email) {
		return customerDao.findByEmail(email);
	}


	@Override
	public Customer findById(long id) {
		return customerDao.findById(id);
	}

	@Override
	public List<Customer> findAll(int offset, int pageSize) {
		return customerDao.findAll(offset, pageSize);
	}

	
}
